package com.wgworkshop.pems.service;

import com.wgworkshop.pems.common.BusinessException;
import com.wgworkshop.pems.common.CommonUtils;
import com.wgworkshop.pems.entity.ClassHourEntity;
import com.wgworkshop.pems.entity.StudentEntity;
import com.wgworkshop.pems.model.ClassHourModel;
import com.wgworkshop.pems.model.ClassHourViewRecord;
import com.wgworkshop.pems.repository.ClassHourRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 课时服务接口实现类。
 * Created by wlc on 2017/7/9.
 */
@Service
public class ClassHourServiceImpl implements ClassHourService {
    @Autowired
    private ClassHourRepository classHourRepository;
    @Autowired
    private StudentService studentService;

    @Override
    public ClassHourModel save(ClassHourModel classHourModel) {
        Long studentId = classHourModel.getStudentId();
        studentService.checkExists(studentId);

        ClassHourEntity classHourEntity = new ClassHourEntity();
        BeanUtils.copyProperties(classHourModel, classHourEntity);

        StudentEntity studentEntity = studentService.getRepository().findOne(studentId);
        classHourEntity.setStudent(studentEntity);

        ClassHourEntity resultEntity = classHourRepository.save(classHourEntity);
        ClassHourModel resultModel = new ClassHourModel();
        BeanUtils.copyProperties(resultEntity, resultModel);
        resultModel.setStudentId(resultEntity.getStudent().getId());

        return resultModel;
    }

    @Override
    public List<ClassHourModel> saveAll(List<ClassHourModel> classHourModels) {
        List<ClassHourModel> resultModels = new ArrayList<>();
        for (ClassHourModel classHourModel : classHourModels) {
            resultModels.add(this.save(classHourModel));
        }
        return resultModels;
    }

    @Override
    public void delete(Long id) {
        checkExists(id);
        classHourRepository.delete(id);
    }

    @Override
    public ClassHourModel update(Long id, ClassHourModel classHourModel) {
        checkExists(id);

        Long studentId = classHourModel.getStudentId();
        StudentEntity studentEntity = null;
        if (studentId != null) {
            studentService.checkExists(studentId);
            studentEntity = studentService.getRepository().findOne(studentId);
        }

        ClassHourEntity classHourEntity = classHourRepository.findOne(id);
        String[] nullProperties = CommonUtils.getNullPropertyNames(classHourModel);
        BeanUtils.copyProperties(classHourModel, classHourEntity, nullProperties);
        if (studentEntity != null) {
            classHourEntity.setStudent(studentEntity);
        }

        ClassHourEntity resultEntity = classHourRepository.save(classHourEntity);
        ClassHourModel resultModel = new ClassHourModel();
        BeanUtils.copyProperties(resultEntity, resultModel);
        resultModel.setStudentId(resultEntity.getStudent().getId());

        return resultModel;
    }

    @Override
    public ClassHourModel update(ClassHourModel classHourModel) {
        Long classHourId = classHourModel.getId();
        checkExists(classHourId);

        Long studentId = classHourModel.getStudentId();
        studentService.checkExists(studentId);

        StudentEntity studentEntity = studentService.getRepository().findOne(studentId);
        ClassHourEntity classHourEntity = classHourRepository.findOne(classHourId);
        BeanUtils.copyProperties(classHourModel, classHourEntity);
        classHourEntity.setStudent(studentEntity);

        ClassHourEntity resultEntity = classHourRepository.save(classHourEntity);
        ClassHourModel resultModel = new ClassHourModel();
        BeanUtils.copyProperties(resultEntity, resultModel);
        resultModel.setStudentId(resultEntity.getStudent().getId());

        return resultModel;
    }

    @Override
    public ClassHourModel findOne(Long id) {
        ClassHourEntity classHourEntity = classHourRepository.findOne(id);
        ClassHourModel classHourModel = null;
        if (classHourEntity != null) {
            classHourModel = new ClassHourModel();
            BeanUtils.copyProperties(classHourEntity, classHourModel);
            classHourModel.setStudentId(classHourEntity.getStudent().getId());
        }

        return classHourModel;
    }

    @Override
    public boolean exists(Long id) {
        return classHourRepository.exists(id);
    }

    @Override
    public void checkExists(Long id) {
        if (!exists(id)) {
            throw new BusinessException(String.format("指定的课时ID不存在[%s]。", id));
        }
    }

    @Override
    public List<ClassHourViewRecord> getClassHourView(String name) {
        Assert.notNull(name, "name must not be null.");
        List<ClassHourViewRecord> viewRecords = new ArrayList<>();
        classHourRepository.getClassHourView(name).forEach(viewRecords::add);
        return viewRecords;
    }

    @Override
    public List<ClassHourModel> findByStudentId(Long id) {
        Assert.notNull(id, "id must not be null.");
        Sort.Order operateDateOrder = new Sort.Order(Sort.Direction.DESC, "operateDate");
        Sort.Order idOrder = new Sort.Order(Sort.Direction.DESC, "id");
        List<Sort.Order> orderList = new ArrayList<>();
        orderList.add(operateDateOrder);
        orderList.add(idOrder);
        Sort sort = new Sort(orderList);
        Iterable<ClassHourEntity> classHourEntities = classHourRepository.findByStudentId(id, sort);
        return CommonUtils.convertIterableToList(classHourEntities, ClassHourModel.class);
    }
}
